I currently have a schema in production (cloudKit and local files) containing non-optional transformable values, e.g.
@Attribute(.transformable(by: TestTransformer.self))
var number: TestTransformable = TestTransformable.init(value: 100)
Unfortunately, this is preventing any migration from succeeding (documented at length in FB22151570).
Briefly summarized, any migration from a Schema containing non-optional transformable values fails between willMigrate and didMigrate with the error "Can't find model for source store". This occurs for all migrations, including lightweight with a migration plan, lightweight without a plan, and custom migrations. Worst of all, this also prevents migration to optional transformable values, or the elimination of the transformable value entirely, leaving us completely stuck.
(note: optional transformable values only work when they have a default value set to nil, otherwise even these have issues migrating)
We already have features being blocked by this issue, and would like to preserve user-data while restoring our ability to move forwards with database.
Are there any known workarounds for using SwiftData (+CloudKit) when schema migration is non-operational?
SwiftData
RSS for tagSwiftData is an all-new framework for managing data within your apps. Models are described using regular Swift code, without the need for custom editors.
Posts under SwiftData tag
200 Posts
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I've understood that SwiftData is not abled to share the whole content of a cloudkit database.
So I'm trying to rewrite everything. Does someone knows id Sharing is coming on SwiftData at WWDC 26?
Anyway, can someone can point me an example a a configured coredata stack that share all its content with other icloud users (with sharing pane and accept invitation code).
At this step, on the owner side, I see some data in the default zone of my private container but nothing is visible on the shared zone. Maybe I don't understand where and when I should check shared data in cloudkit console. Need Help also here.
See below by configuration stack:
// Core Data container
public lazy var container: NSPersistentContainer = {
switch delegate.usage() {
case .preview : return previewContainer()
case .local : return localContainer()
case .cloudKit : return cloudKitContainer()
}
}()
private func cloudKitContainer() -> NSPersistentContainer {
let modelURL = delegate.modelURL()
let modelName = modelURL.deletingPathExtension().lastPathComponent
guard let model = NSManagedObjectModel(contentsOf: modelURL) else {
fatalError("Could not load Core Data model from \(modelURL)")
}
let container = NSPersistentCloudKitContainer(
name: modelName,
managedObjectModel: model
)
let groupIdentifier = AppManager.shared.groupIdentifier
guard let appGroupURL = FileManager.default.containerURL (
forSecurityApplicationGroupIdentifier: groupIdentifier
) else {
fatalError("App Group not found: \(groupIdentifier)")
}
// MARK: - Private Store Configuration
let privateStoreURL = appGroupURL.appendingPathComponent("\(modelName).sqlite")
let privateStoreDescription = NSPersistentStoreDescription(url: privateStoreURL)
// Persistent history tracking (MANDATORY)
privateStoreDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
privateStoreDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
// CloudKit options for private database
// Core Data automatically uses the default zone: com.apple.coredata.cloudkit.zone
let privateCloudKitOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: delegate.cloudKitIdentifier())
privateCloudKitOptions.databaseScope = .private
privateStoreDescription.cloudKitContainerOptions = privateCloudKitOptions
// MARK: - Shared Store Configuration
guard let sharedStoreDescription = privateStoreDescription.copy() as? NSPersistentStoreDescription else {
fatalError("Create shareDesc error")
}
// The shared store receives zones that others share with us via CloudKit's shared database
sharedStoreDescription.url = appGroupURL.appendingPathComponent("\(modelName)-shared.sqlite")
// Persistent history tracking (MANDATORY)
sharedStoreDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
sharedStoreDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
// CloudKit options for shared database
// This syncs data from CloudKit shared zones when we accept share invitations
let sharedCloudKitOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: delegate.cloudKitIdentifier())
sharedCloudKitOptions.databaseScope = .shared
sharedStoreDescription.cloudKitContainerOptions = sharedCloudKitOptions
// Configure both stores
// Private store: com.apple.coredata.cloudkit.zone in private database
// Shared store: Receives shared zones we're invited to
container.persistentStoreDescriptions = [privateStoreDescription, sharedStoreDescription]
container.loadPersistentStores { storeDescription, error in
if let error = error as NSError? {
fatalError("DB init error:\(error.localizedDescription)")
} else if let cloudKitContiainerOptions = storeDescription.cloudKitContainerOptions {
switch cloudKitContiainerOptions.databaseScope {
case .private:
self._privatePersistentStore = container.persistentStoreCoordinator.persistentStore(for: privateStoreDescription.url!)
case .shared:
self._sharedPersistentStore = container.persistentStoreCoordinator.persistentStore(for: sharedStoreDescription.url!)
default:
break
}
}
let scope = storeDescription.cloudKitContainerOptions?.databaseScope == .shared ? "shared" : "private"
print("✅ \(scope) store loaded at: \(storeDescription.url?.path ?? "unknown")")
}
// Auto-merge
container.viewContext.automaticallyMergesChangesFromParent = true
container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
do {
try container.viewContext.setQueryGenerationFrom(.current)
} catch {
fatalError("Fail to pin viewContext to the current generation:\(error)")
}
return container
}
Topic:
App & System Services
SubTopic:
iCloud & Data
Tags:
CloudKit
Core Data
CloudKit Console
SwiftData
Hello, I am attempting to implement a simple button that loads persistent data from a class (see below).
Button("Reload Data") {
while tableData2.isEmpty == false{
tableData2.remove(at: 0)
}
while tableView.isEmpty == false{
tableView.remove(at: 0)
}
//update
if csvData.isEmpty == false{
for superRow in csvData[0].tableData2{
tableData2.append(superRow)
}
for supperRow in csvData[0].tableView{
tableView.append(supperRow)
}
print("Item at 0: \(csvData[0].tableData2[[0][0]])")
print("\(tableData2[0][0])")
} else {
print("csvData is empty")
}
}
This button DOES work to appropriately print the data stored at the printed location once loaded in initially. The problem is that it doesn’t work across app restarts, which is the whole point of the button. Below I will include the rest of the relevant code with notes:
In contentView declaring the persistant variables:
Query private var csvData: [csvDataPersist]
Environment(\.modelContext) private var modelContext
The simple class of data I’m storing/pulling to/from:
@Model
class csvDataPersist{
var tableView: [[String]] = []
var tableData2: [[String]] = []
init(tableView: [[String]], tableData2: [[String]]) {
self.tableView = tableView
self.tableData2 = tableData2
}
}
In (appname)App: I tried both locations of the model container but it didn’t seem to matter
var body: some Scene {
WindowGroup {
ContentView()
.modelContainer(for: csvDataPersist.self)
}
//.modelContainer(for: csvDataPersist.self)
.modelContainer(sharedModelContainer)
}
How I’m attempting to save the data:
let newCSVDataPersist = csvDataPersist(tableView: tableView, tableData2: tableData2)
//modelContext.rollback()
//for superrow in csvData.count{
// csvData[superrow].tableData2.removeAll()
//}
//modelContext.rollback()
//csvData[0].tableData2.removeAll()
//csvData[0].tableView.removeAll()
if csvData.isEmpty == false {
print("not empty, deleting prev data")
modelContext.delete(csvData[0])
} else {
print("it empty, load data.")
}
modelContext.insert(newCSVDataPersist)
//try modelContext.save()
Note that I’ve tried just about every combination of enabling and disabling the commented out lines. This is the part of the code I am the least confident in, but after trying for hours to troubleshoot on my own I would appreciate any input from the community.
Something else that may be of note, in a previous attempt, upon a second startup, the terminal would print:
"coredata: error: error: persistent history (random number) has to be truncated due to the following entities being removed: ( csvdatapersist )"
Why is csvDataPersist getting removed? What is it getting removed by? Looking up this error was fruitless. Most sites instructed me to basically hard reset my simulators, clean the build, restart my computer, and try again. I've done all of these things about a hundred times at this point, with no results.
Any help would be much appreciated!
I’m developing an app using SwiftData. In my app, I have two models: User and Address. A user can have multiple addresses. I’m trying to use SwiftData History tracking to implement some logic when addresses are deleted. Specifically, I need to determine which user the address belonged to. From the documentation, I understand that you can preserve attributes from deleted models in a tombstone object using @Attribute(.preserveValueOnDeletion). However, this isn’t working when I try to apply this to a relationship value. Below is a simplified example of my attempts so far. I suspect that simply adding @Attribute(.preserveValueOnDeletion) to a relationship isn’t feasible. If that’s indeed the case, what would be the recommended approach to identify the user associated with an address after it has been deleted? Thank you.
@Model class User {
var name: String
@Relationship(deleteRule: .cascade, inverse: \Address.user) var addresses: [Address] = []
init(name: String) {
self.name = name
}
}
@Model class Address {
var adress1: String
var address2: String
var city: String
var zip: String
@Attribute(.preserveValueOnDeletion)
var user: User?
init(adress1: String, address2: String, city: String, zip: String) {
self.adress1 = adress1
self.address2 = address2
self.city = city
self.zip = zip
self.user = user
}
}
for transaction in transactions {
for change in transaction.changes {
switch change {
case .delete(let deleted):
if let deleted = deleted as? any HistoryDelete<Address> {
if let user = deleted.tombstone[\.user] {
//this is never executed
}
}
default:
break
}
}
}
Hi everyone
I would like to achieve having unidirectional relationships in my SwiftData project (which I believe is possible: https://developer.apple.com/documentation/updates/swiftdata?changes=_9) but I'm afraid I'm struggling to overcome the errors I'm experiencing.
For example, I have the following models:
@Model
final class Quota {
@Attribute(.unique) var id: UUID
var allowance: Int
@Relationship(inverse: nil) var fish: Fish
init(id: UUID = UUID(), fish: Fish, allowance: Int) {
self.id = id
self.fish = fish
self.allowance = allowance
}
}
@Model
final class Fish {
@Attribute(.unique) var id: Int
var name: String
init(id: Int, name: String) {
self.id = id,
self.name = name
}
}
However, when I attempt to save a quota as so:
let quota: Quota = .init(fish: Fish(id: 2, name: "Salmon"), allowance: 50)
modelContext?.insert(quota)
try save()
I keep getting the following error:
SwiftData.DefaultStore save failed with error: Error Domain=NSCocoaErrorDomain Code=1570 "%{PROPERTY}@ is a required value." UserInfo={NSValidationErrorObject=<NSManagedObject: 0x600002217390> (entity: Fish; id: 0x83319d001151328d <x-coredata://C76A2A64-146E-432F-A565-319B5A2F23F5/Fish/p12>; data: { id = nil; }), NSLocalizedDescription=%{PROPERTY}@ is a required value., NSValidationErrorKey=id, NSValidationErrorValue=null} %{PROPERTY}@ is a required value.
However, if I set up Quota and Fish with an inverse relationship then the data saves as expected, so I'm a little confused. Is there anyone out there who can provide some guidance as to why I'm seeing this error when I try to save a record in SwiftData with no inverse relationship?
I do fully understand about unidirectional vs bidirectional relationships but I have a scenario where I need the relationship to be unidirectional. Also, as a side note, the Fish record already exists in my database, but if I delete it and try to save the record I still see this error.
Thank you so much in advance for any help.
I was hoping for an update of SwiftData which adopted the use of shared and public CloudKit containers, in the same way it does for the private CloudKit container.
So firstly, a big request to any Apple devs reading, for this to be a thing!
Secondly, what would be a sensible way of adding a shared container in CloudKit to an existing app that is already using SwiftData?
Would it be possible to use the new DataStore method to manage CloudKit syncing with a public or shared container?
I have an app with fairly typical requirements - I need to insert some data (in my case from the network but could be anything) and I want to do it in the background to keep the UI responsive.
I'm using SwiftData.
I've created a ModelActor that does the importing and using the debugger I can confirm that the data is indeed being inserted.
On the UI side, I'm using @Query and a SwiftUI List to display the data but what I am seeing is that @Query is not updating as the data is being inserted. I have to quit and re-launch the app in order for the data to appear, almost like the context running the UI isn't communicating with the context in the ModelActor.
I've included a barebones sample project. To reproduce the issue, tap the 'Background Insert' button. You'll see logs that show items being inserted but the UI is not showing any data.
I've tested on the just released iOS 18b3 seed (22A5307f).
The sample project is here:
https://hanchor.s3.amazonaws.com/misc/SwiftDataBackgroundV2.zip
I'm building an app that heavily relies on EKEventStore for calendar and reminder integration. The API is simple - and limited.
Change notifications amount to "something changed, you'd better refetch everything you care about." There's no way to know whether the calendar was updated while your app was closed or backgrounded. EKEvents and EKReminders don't trigger SwiftUI view updates, so you end up shunting them into your own observable state and keeping the two in sync.
My app is fairly complex rendering-wise, and I lament being locked into treating EKEventStore as a first-class citizen of my view and data layer. It makes everything clunkier, essentially shuts the door on modern features like undo/redo, and makes integrating with other calendar providers that much harder.
I'm exploring a custom SwiftData DataStore ↔ EKEventStore sync engine, but this is no easy task. There are still many unknowns I'd need to spike out before I can even attempt a proper implementation.
Still, I'm curious - is this something being actively worked on behind the scenes? Will we see a more modern, observable, SwiftUI-native EventKit integration in the future?
I'm trying to determine if this is "expected" swiftui behavior or an issue with SwiftUI/Data which needs a feedback request...
When a child view has a Query containing a filter predicate, the query is run with each and every edit of the parent view, even when the edit has no impact on the child view (e.g. bindings not changing).
In the example below, ContentView has the TextField name, and while data is being entered in it, causes the Query in AddTestStageView to be run with each character typed, e.g. 30 characters result in 30 query executions.
(Need "-com.apple.CoreData.SQLDebug 1" launch argument to see SQL output).
Removing the filter predicate from the query and filtering in ForEach prevents the issue.
In my actual use case, the query has a relatively small result set (<100 rows), but I can see this as a performance issue with the larger result sets.
xcode/ios: 26.2
Repro example code:
import SwiftUI
import SwiftData
// Repro to Query filter issue in child view running multiple time unexpectedly
// Need "-com.apple.CoreData.SQLDebug 1" launch argument set to see SQL console output.
@main
struct ReproViewQueryMultipleRunningsApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(DataManager.shared.sharedModelContainer())
}
}
@Model
final class TestStageClass {
var id: UUID = UUID()
var name: String = ""
var isActive: Bool = true
var displayOrder: Int = 0
init(name: String, isActive: Bool, displayOrder: Int) {
self.name = name
self.isActive = isActive
self.displayOrder = displayOrder
}
}
struct ContentView: View {
@Environment(\.modelContext) var modelContext
@State private var name: String = ""
@State private var selectedTestStage: TestStageClass = DataManager.shared.getFirstTestStageClass()
var body: some View {
VStack (spacing: 20) {
TextField("Name", text: $name)
AddTestStageView(selectedTestStage: $selectedTestStage)
}
.frame(height: 200)
}
}
#Preview("Sample Data") {
ContentView()
.modelContainer(DataManager.shared.sharedModelContainer())
}
struct AddTestStageView: View {
@Environment(\.modelContext) var modelContext
@Binding var selectedTestStage: TestStageClass
// MARK: - ISSUE LOCATION
/// Using this Query with filter causes it to be run after each editing on parent view - such as each letter when editing a name.
@Query(filter: #Predicate<TestStageClass> { $0.isActive }) private var testStageClasses: [TestStageClass]
/// Using this query doesn't have the issue, then need filter in ForEach.
// @Query() private var testStageClasses: [TestStageClass]
var body: some View {
Picker("stage", selection: $selectedTestStage) { // filter and sort here does not affect issue with above Query predicate filter.
ForEach(testStageClasses.filter(\.isActive).sorted(by: { $0.displayOrder < $1.displayOrder } ), id: \.id) { stage in
Text("\(stage.name)")
.tag(stage)
}
}
}
}
class DataManager {
static let shared = DataManager()
private var modelContainer: ModelContainer? = nil
public func sharedModelContainer(inMemory: Bool = false) -> ModelContainer {
let schema = Schema([TestStageClass.self])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: inMemory)
do {
self.modelContainer = try ModelContainer(for: schema, configurations: [modelConfiguration])
checkDataExists()
return self.modelContainer!
} catch {
fatalError("Could not create sharedModelContainer. Schema:\(schema.entities.map(\.name)), \((modelConfiguration.isStoredInMemoryOnly) ? "in memory only" : "in disk"):\n\(error.localizedDescription)")
}
}
private func checkDataExists() {
let mainContext = self.modelContainer!.mainContext
print("checkDataExists")
do {
let classData: [TestStageClass] = try mainContext.fetch(FetchDescriptor<TestStageClass>())
if classData.isEmpty {
mainContext.insert(TestStageClass(name: "Beginning", isActive: true, displayOrder: 0))
mainContext.insert(TestStageClass(name: "Second Middle", isActive: false, displayOrder: 2))
mainContext.insert(TestStageClass(name: "Middle", isActive: true, displayOrder: 1))
mainContext.insert(TestStageClass(name: "End", isActive: true, displayOrder: 3))
}
if mainContext.hasChanges {
try? mainContext.save()
print("Added Default Data for TestStageClass")
}
} catch {
fatalError("Failed to get item count for TestStageClass: \(error.localizedDescription)")
}
}
func getFirstTestStageClass() -> TestStageClass {
let mainContext = self.modelContainer!.mainContext
var tmp: TestStageClass?
do {
let classData: [TestStageClass] = try mainContext.fetch(FetchDescriptor<TestStageClass>())
tmp = classData.sorted(by: {$0.displayOrder < $1.displayOrder }).first
} catch {
fatalError("getFirstTestStageClass: \(error.localizedDescription)")
}
return tmp!
}
}
Thanks,
Steve
Hi everyone,
On macOS 26.4 beta (with Xcode 26.4 beta), I’m seeing the following console messages in a brand new SwiftData + CloudKit template project (no custom logic added, fresh CloudKit container):
updateTaskRequest called for a pre-running task com.apple.coredata.cloudkit.activity.export.F9EE783D-7521-4EC2-B42C-9FD1F29BA5C4
updateTaskRequest called for an already running/updated task com.apple.coredata.cloudkit.activity.export.F9EE783D-7521-4EC2-B42C-9FD1F29BA5C4
Error updating background task request: Error Domain=BGSystemTaskSchedulerErrorDomain Code=8 "(null)"
These messages appear:
When CloudKit is enabled
Occasionally on app launch
Often when bringing the app back to the foreground (Cmd-Tab away and back)
Even with zero additional SwiftData logic
They do not appear when CloudKit is disabled.
This behavior is reproducible on a completely new project with a fresh CloudKit container.
Questions:
What exactly do these messages indicate?
Is BGSystemTaskScheduler Code=8 expected in this context?
Are these safe to ignore?
Is this a known change in logging behavior in macOS 26.4 beta?
Additionally, in a larger project I’ve observed SwiftData crashes and initially suspected these logs might be related. However, since the issue reproduces in a fresh template project, I’m unsure whether this is simply verbose beta logging or something more serious.
Any clarification would be appreciated.
Filed as FB21993521.
I've spent a few months writing an app that uses SwiftData with inheritance. Everything worked well until I tried adding CloudKit support. To do so, I had to make all relationships optional, which exposed what appears to be a bug. Note that this isn't a CloudKit issue -- it happens even when CloudKit is disabled -- but it's due to the requirement for optional relationships.
In the code below, I get the following error on the second call to modelContext.save() when the button is clicked:
Could not cast value of type 'SwiftData.PersistentIdentifier' (0x1ef510b68) to 'SimplePersistenceIdentifierTest.Computer' (0x1025884e0).
I was surprised to find zero hit when Googling "Could not cast value of type 'SwiftData.PersistentIdentifier'".
Some things to note:
Calling teacher.computers?.append(computer) instead of computer.teacher = teacher results in the same error.
It only happens when Teacher inherits Person.
It only happens if modelContext.save() is called both times.
It works if the first modelContext.save() is commented out.
If the second modelContext.save()is commented out, the error occurs the second time the model context is saved (whether explicitly or implicitly).
Keep in mind this is a super simple repro written to generate on demand the error I'm seeing in a normal app. In my app, modelContext.save() must be called in some places to update the UI immediately, sometimes resulting in the error seconds later when the model context is saved automatically. Not calling modelContext.save() doesn't appear to be an option.
To be sure, I'm new to this ecosystem so I'd be thrilled if I've missed something obvious! Any thoughts are appreciated.
import Foundation
import SwiftData
import SwiftUI
struct ContentView: View {
@Environment(\.modelContext) var modelContext
var body: some View {
VStack {
Button("Do it") {
let teacher = Teacher()
let computer = Computer()
modelContext.insert(teacher)
modelContext.insert(computer)
try! modelContext.save()
computer.teacher = teacher
try! modelContext.save()
}
}
}
}
@Model
class Computer {
@Relationship(deleteRule: .nullify)
var teacher: Teacher?
init() {}
}
@Model
class Person {
init() {}
}
@available(iOS 26.0, macOS 26.0, *)
@Model
class Teacher: Person {
@Relationship(deleteRule: .nullify, inverse: \Computer.teacher)
public var computers: [Computer]? = []
override init() {
super.init()
}
}
Hello, I've a question about performance when trying to render lots of items coming from SwiftData via a @Query on a SwiftUI List. Here's my setup:
// Item.swift:
@Model final class Item: Identifiable {
var timestamp: Date
var isOptionA: Bool
init() {
self.timestamp = Date()
self.isOptionA = Bool.random()
}
}
// Menu.swift
enum Menu: String, CaseIterable, Hashable, Identifiable {
var id: String { rawValue }
case optionA
case optionB
case all
var predicate: Predicate<Item> {
switch self {
case .optionA: return #Predicate { $0.isOptionA }
case .optionB: return #Predicate { !$0.isOptionA }
case .all: return #Predicate { _ in true }
}
}
}
// SlowData.swift
@main
struct SlowDataApp: App {
var sharedModelContainer: ModelContainer = {
let schema = Schema([Item.self])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
return try! ModelContainer(for: schema, configurations: [modelConfiguration])
}()
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(sharedModelContainer)
}
}
// ContentView.swift
struct ContentView: View {
@Environment(\.modelContext) private var modelContext
@State var selection: Menu? = .optionA
var body: some View {
NavigationSplitView {
List(Menu.allCases, selection: $selection) { menu in
Text(menu.rawValue).tag(menu)
}
} detail: {
DemoListView(selectedMenu: $selection)
}.onAppear {
// Do this just once
// (0..<15_000).forEach { index in
// let item = Item()
// modelContext.insert(item)
// }
}
}
}
// DemoListView.swift
struct DemoListView: View {
@Binding var selectedMenu: Menu?
@Query private var items: [Item]
init(selectedMenu: Binding<Menu?>) {
self._selectedMenu = selectedMenu
self._items = Query(filter: selectedMenu.wrappedValue?.predicate,
sort: \.timestamp)
}
var body: some View {
// Option 1: touching `items` = slow!
List(items) { item in
Text(item.timestamp.description)
}
// Option 2: Not touching `items` = fast!
// List {
// Text("Not accessing `items` here")
// }
.navigationTitle(selectedMenu?.rawValue ?? "N/A")
}
}
When I use Option 1 on DemoListView, there's a noticeable delay on the navigation. If I use Option 2, there's none. This happens both on Debug builds and Release builds, just FYI because on Xcode 16 Debug builds seem to be slower than expected: https://indieweb.social/@curtclifton/113273571392595819
I've profiled it and the SwiftData fetches seem blazing fast, the Hang occurs when accessing the items property from the List. Is there anything I'm overlooking or it's just as fast as it can be right now?
I'm using SwiftData with CloudKit private database. I was able to identify the error on my device by debugging in Xcode with com.apple.CoreData.SQLDebug flag. However, in Production, I couldn't find a way to get the cause of errors.
I tried inspecting the error coming from eventChangedNotification. The NSPersistentCloudKitContainer.Event error does not contain any underlying error (neither CKError.userInfo nor in NSError.underlyingError). It only reports a partial failure with CKErrorDomain code 2.
If a user encounter an error, there seems to be no way to retrieve the error details.
Is there any way to access the error details or logs in Production?
I am new to swiftui and have a very small project I am working on to practice passing data between views. I have this error on a form "Trailing closure passed to parameter of type 'FormStyleConfiguration' that does not accept a closure"
If I comment the first section in the form then I get three errors in the ForEach. If anyone can help explain what is going on and what steps I could take to determine where the problem is coming from I would appreciate the help.
This is the model I created:
import Observation
import SwiftUI
@Model
final class Client {
var id = UUID()
var name: String
var location: String
var selectedJob: Person
init(id: UUID = UUID(), name: String, location: String, selectedJob: Person) {
self.id = id
self.name = name
self.location = location
self.selectedJob = selectedJob
}
}
extension Client {
enum Person: String, CaseIterable, Codable {
case homeOwner = "Home Owner"
case contractor = "Contractor"
case designer = "Designer"
}
}
@Model
class Enclosure {
var id = UUID()
var room: String = ""
var unitType: String = ""
init(id: UUID = UUID(), room: String, unitType: String) {
self.id = id
self.room = room
self.unitType = unitType
}
}
This is the detail view where the error is happening:
import SwiftData
import SwiftUI
struct DetailView: View {
@Environment(\.modelContext) private var modelContext
@Environment(\.dismiss) private var dismiss
@Query(sort: \Enclosure.room, order: .forward, animation: .default) private var enclosures: [Enclosure]
@State private var showingAddEnclosure = false
@State private var showingAddMirror = false
@State private var name: String = ""
@State private var location: String = ""
@State private var selectedJob = Client.Person.homeOwner
@State var clients: Client
var body: some View {
NavigationStack {
Form {
Section("Details") {
TextField("Full Name", text: Client.$name)
TextField("Location", text: Client.location)
Picker("Job Type", selection: $selectedJob) {
ForEach(Client.Person.allCases, id: \.self) { selected in
Text(selected.rawValue).tag(selected)
}
}
}
Section("Enclosures") {
List {
ForEach($clients.enclosures) { enclosure in
NavigationLink(destination: EnclosureDetailView()) {
VStack {
Text(enclosure.room)
Text(enclosure.unitType)
}
}
.swipeActions {
Button("Delete", role: .destructive) {
modelContext.delete(enclosure)
}
}
}
}
}
}
.navigationTitle("Project Details")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Menu {
Button {
showingAddEnclosure.toggle()
} label: {
Text("Add Enclosure")
}
Button {
showingAddMirror.toggle()
} label: {
Text("Add Mirror")
}
} label: {
Label("Add", systemImage: "ellipsis.circle")
}
}
}
.sheet(isPresented: $showingAddEnclosure) {
EnclosureView()
}
.sheet(isPresented: $showingAddMirror) {
EnclosureView()
}
}
}
}
Hey everyone I just ran into an issue where I couldn't sync the model below fully by using CloudKit,
enum LinkMapV3_1: VersionedSchema {
static let versionIdentifier: Schema.Version = .init(3, 1, 0)
static var models: [any PersistentModel.Type] {
[AnnotationData.self, GroupData.self, Item.self, Deployment.self, History.self]
}
// MARK: - Data
@Model
class AnnotationData {
var name: String = ""
var longitude: Double = 0.0
var latitude: Double = 0.0
var order: Int = -1
var level: Int = 1
var detail: String = ""
@Relationship(deleteRule: .nullify, inverse: \GroupData.annotation)
var groups: [GroupData]?
@Relationship(deleteRule: .nullify, inverse: \AnnotationData.to)
var from: AnnotationData?
var to: AnnotationData?
var history: History?
}
// MARK: - History
@Model
class History {
var id: UUID = UUID()
var timestamp: Date = Date()
@Relationship(deleteRule: .nullify, inverse: \AnnotationData.history)
var annotations: [AnnotationData]?
@Relationship(deleteRule: .nullify, inverse: \GroupData.history)
var groups: [GroupData]?
@Relationship(deleteRule: .nullify, inverse: \Item.history)
var items: [Item]?
@Relationship(deleteRule: .nullify, inverse: \Deployment.history)
var deployment: Deployment?
var formattedDate: String {
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .short
return formatter.string(from: timestamp)
}
var timeAgo: String {
let formatter = RelativeDateTimeFormatter()
formatter.unitsStyle = .abbreviated
return formatter.localizedString(for: timestamp, relativeTo: Date())
}
}
}
So when trying to sync with the code in documentation
let modelContainer: ModelContainer
init() {
let config = ModelConfiguration()
typealias vs = LinkMapV3_1
do {
#if DEBUG
// Use an autorelease pool to make sure Swift deallocates the persistent
// container before setting up the SwiftData stack.
try autoreleasepool {
let desc = NSPersistentStoreDescription(url: config.url)
let opts = NSPersistentCloudKitContainerOptions(containerIdentifier: "iCloud.name.Endsunset.LinkMap.SwiftData.v1")
desc.cloudKitContainerOptions = opts
// Load the store synchronously so it completes before initializing the
// CloudKit schema.
desc.shouldAddStoreAsynchronously = false
if let mom = NSManagedObjectModel.makeManagedObjectModel(for: [vs.AnnotationData.self, vs.GroupData.self, vs.Item.self, vs.Deployment.self, vs.History.self]) {
let container = NSPersistentCloudKitContainer(name: "LinkMap", managedObjectModel: mom)
container.persistentStoreDescriptions = [desc]
container.loadPersistentStores {_, err in
if let err {
fatalError(err.localizedDescription)
}
}
// Initialize the CloudKit schema after the store finishes loading.
try container.initializeCloudKitSchema()
// Remove and unload the store from the persistent container.
if let store = container.persistentStoreCoordinator.persistentStores.first {
try container.persistentStoreCoordinator.remove(store)
}
}
}
#endif
modelContainer = try ModelContainer(for:
vs.AnnotationData.self,
vs.GroupData.self,
vs.Item.self,
vs.Deployment.self,
vs.History.self,
configurations: config)
} catch {
fatalError(error.localizedDescription)
}
}
The output is
Console Output
Where you can see
Output Extract
Optional arrays with @Relationship are missing, and the entry of record types on cloudkit database container are also missing it.
When I attempt to insert an annotation, I got
SwiftData/PersistentModel.swift:559: Fatal error: This KeyPath does not appear to relate AnnotationData to anything - \AnnotationData.groups
It gets more suspicious when restart the app and try again, the above error end with "AnnotationData.history", and if I tried again the above error end with "AnnotationData.from"... and so on.
No matter how my app stop working.
I'm setting up App Entities for my SwiftData models and I'm not sure about the best way to reference SwiftData model properties in the AppEntity.
I have a SwiftData model with many properties:
@Model
final class Contact {
@Attribute(.unique) var id: UUID = UUID()
var name: String
var phoneNumber: String
var email: String
var website: URL?
var birthday: Date?
var notes: String
// ... many more properties
}
I want to expose these properties on my AppEntity so they're available for system features, such as giving Apple Intelligence more context about on-screen content.
struct ContactEntity: AppEntity {
var id: UUID
@Property(title: "Name")
var name: String
@Property(title: "Phone")
var phoneNumber: String
@Property(title: "Email")
var email: String
// ... all the other properties
}
I couldn't find guidance in the documentation for this specific situation. I've considered two approaches:
Add @Property variables to the AppEntity for each SwiftData model property and copy all values from the SwiftData model to the AppEntity in the AppEntity initializer — but I recall this being discouraged in previous WWDC sessions since it duplicates data and can become stale
Use @ComputedProperty to fetch the model and access the single properties — this seems like an alternative, but fetching the entire model just to access individual properties doesn't feel right
What is the recommended approach when SwiftData is the data source?
Thank you!
Hi all,
I have setup my app to use SwiftData with CloudKit sync. I have a production environment and development environment. I can reset the development environment for myself and all users in CloudKit console, but I can't reset the production one as it's tried to users' iCloud accounts, so I've added a button in-app for that feature. In the onboarding of my app, I pre-seed the DB with some default objects, which should be persisted between app install. The issue I'm running into is that I'm unable to force-pull these models from iCloud during the onboarding of a clean re-install, which leads to the models later appearing as duplicates once the user has been on the app for a few minutes and it has pulled from their iCloud account. If anyone has any suggestions on how to handle this issue, I would greatly appreciate it.
I have SwiftData models containing arrays of Codable structs that worked fine before adding CloudKit capability. I believe they are the reason I started seeing errors after enabling CloudKit.
Example model:
@Model
final class ProtocolMedication {
var times: [SchedulingTime] = [] // SchedulingTime is Codable
// other properties...
}
After enabling CloudKit, I get this error logged to the console:
'NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release
CloudKit Console shows this times data as "plain text" instead of "bplist" format.
Other struct/enum properties display correctly (I think) as "bplist" in CloudKit Console.
The local SwiftData storage handled these arrays fine - this issue only appeared with CloudKit integration.
What's the recommended approach for storing arrays of Codable structs in SwiftData models that sync with CloudKit?
Hi everyone,
In the simple app below, I have a QueryView that has LazyVStack containing 100k TextField's that edit the item's content. The items are fetched with a @Query. On launch, the app will generate 100k items. Once created, when I press any of the TextField's , a severe hang happens, and every time I type a single character, it will cause another hang over and over again.
I looked at it in Instruments and it shows that the main thread is busy during the duration of the hang (2.31 seconds) updating QueryView. From the cause and effect graph, the update is caused by @Observable QueryController <Item>.(Bool).
Why does it take too long to recalculate the view, given that it's in a LazyVStack? (In other words, why is the hang duration directly proportional to the number of items?)
How to fix the performance of this app? I thought adding LazyVStack was all I need to handle the large dataset, but maybe I need to add a custom pagination with .fetchLimit on top of that? (I understand that ModelActor would be an alternative to @Query because it will make the database operations happen outside of the main thread which will fix this problem, but with that I will lose the automatic fetching of @Query.)
Thank you for the help!
import SwiftData
import SwiftUI
@main
struct QueryPerformanceApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.modelContainer(for: [Item.self], inMemory: true)
}
}
}
@Model
final class Item {
var name: String
init(name: String) {
self.name = name
}
}
struct ItemDetail: View {
@Bindable var item: Item
var body: some View {
TextField("Name", text: $item.name)
}
}
struct QueryView: View {
@Query private var items: [Item]
var body: some View {
ScrollView {
LazyVStack {
ForEach(items) { item in
VStack {
ItemDetail(item: item)
}
}
}
}
}
}
struct ContentView: View {
let itemCount = 100_000
@Environment(\.modelContext) private var context
@State private var isLoading = true
var body: some View {
Group {
if isLoading {
VStack(spacing: 16) {
ProgressView()
Text("Generating \(itemCount) items...")
}
} else {
QueryView()
}
}
.task {
for i in 1...itemCount {
context.insert(Item(name: "Item \(i)"))
}
try? context.save()
isLoading = false
}
}
}
I want to use the Observations AsyncSequence on some SwiftData @Model instances to determine if internal calculations need to be done.
When a simple property is linked to the Observations it fires CONTINUOUSLY even though no change is made to the model property.
Also, when I try to observe a property which is a list of another @Model type the Observations sequence does not fire when I add or remove items.
I am hoping to use the async-algorithm's merge function so all the associated sequences can be combined since if any of the associated events should fire the calculation event.